local redis = require 'resty.redis'
local ngx =require "ngx"
local commonsConf = require "hgg-lua.commonsConf"

local _M={}

local function setRedisPool(client)
    local ok,err=client:set_keepalive(10000,50)  --Redis连接放入连接池
    if not ok then
        client:close();-- 关闭连接
    end    
end


_M.get=function (redisKey)
    local redisClient,err = redis:new()
    if not redisClient then
        return redisClient,"获取Redis实例对象异常："..redisClient
    end
    redisClient:set_timeout(commonsConf.redis_conf.redis_timeout)
    -- 创建一个Redis连接对象
    local ok,err = redisClient:connect(commonsConf.redis_conf.redis_host, commonsConf.redis_conf.redis_port)
    if not ok then
        return ok,"连接Redis异常："..err
    end
    
    local ok,err=redisClient:auth(commonsConf.redis_conf.redis_password)
    if not ok then
        return ok,"连接Redis密码验证异常："..err
    end
    -- 获取键的值
    local value,err = redisClient:get(redisKey)
    
    if not value then  
        value="读取Redis异常："..err
    end
    setRedisPool(redisClient)
    return ok,value
end

return _M